package com.hr.system.controller;

import java.util.List;

import com.hr.common.core.domain.Result;
import com.hr.common.utils.DateUtils;
import com.hr.common.utils.ResultUtil;
import com.hr.common.utils.ShiroUtils;
import com.hr.system.domain.ClockRule;
import com.hr.system.domain.SysPersonnelClock;
import com.hr.system.utils.ClockUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.hr.common.annotation.Log;
import com.hr.common.enums.BusinessType;
import com.hr.system.service.ISysPersonnelClockService;
import com.hr.common.core.controller.BaseController;
import com.hr.common.core.domain.AjaxResult;
import com.hr.common.utils.poi.ExcelUtil;
import com.hr.common.core.page.TableDataInfo;

/**
 * Controller
 * 
 * @author ruoyi
 * @date 2021-01-18
 */
@Controller
@RequestMapping("/system/clock")
public class SysPersonnelClockController extends BaseController
{
    private String prefix = "system/clock";

    @Autowired
    private ISysPersonnelClockService sysPersonnelClockService;

    @GetMapping()
    public String clock()
    {
        return prefix + "/clock";
    }

    @GetMapping("/manage")
    public String clockManage()
    {
        return prefix + "/manage";
    }

    /**
     * 查询
     */
    @RequiresPermissions("system:clock:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysPersonnelClock personnelClock)
    {
        startPage();
        SysPersonnelClock peronnelClockBean = ClockUtils.getPeronnelClockBean();
        List<SysPersonnelClock> list = sysPersonnelClockService.selectSysPersonnelClockList(peronnelClockBean);
        return getDataTable(list);
    }

    /**
     * 打卡管理查询
     */
    @RequiresPermissions("system:clock:manage")
    @PostMapping("/manageList")
    @ResponseBody
    public TableDataInfo manageList(SysPersonnelClock personnelClock)
    {
        startPage();
        personnelClock.setTenantId(ShiroUtils.getTenantId());
        List<SysPersonnelClock> list = sysPersonnelClockService.selectSysPersonnelClockList(personnelClock);
        return getDataTable(list);
    }


    /**
     * 导出
     */
    @RequiresPermissions("system:clock:export")
    @Log(title = "【导出打卡】", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SysPersonnelClock personnelClock)
    {
        //设置自己的打卡数据
        SysPersonnelClock peronnelClockBean = ClockUtils.getPeronnelClockBean();
        List<SysPersonnelClock> list = sysPersonnelClockService.selectSysPersonnelClockList(peronnelClockBean);
        ExcelUtil<SysPersonnelClock> util = new ExcelUtil<SysPersonnelClock>(SysPersonnelClock.class);
        return util.exportExcel(list, "clock");
    }

    /**
     * 打卡
     */
    @ResponseBody
    @GetMapping("/daka")
    public Result<SysPersonnelClock> clock(SysPersonnelClock sysPersonnelClock) {
        return sysPersonnelClockService.insertSysPersonnelClock(sysPersonnelClock);
    }



    /**
     * 到异常打卡页面
     * @return
     */
    @GetMapping("/toAbnormalClocks")
    public String toAbnormalClocks()
    {
        return prefix + "/abnormalClock";
    }


    /**
     * 查询异常打卡信息
     */
    @PostMapping("/listAbnormalClocks")
    @ResponseBody
    public TableDataInfo listAbnormalClocks(SysPersonnelClock abNormalPersonnelClock)
    {
        startPage();
        abNormalPersonnelClock.setPersonnelId(ShiroUtils.getSysUser().getUserId().intValue());
        abNormalPersonnelClock.setTenantId(ShiroUtils.getSysUser().getTenantId());
        List<SysPersonnelClock> list = sysPersonnelClockService.selectSysPersonnelAbnormalClocks(abNormalPersonnelClock);
        return getDataTable(list);
    }

    @GetMapping("/updateCurrentDateClocks")
    @ResponseBody
    public Result<SysPersonnelClock> getCurrentDateClock(){
        ClockRule clockRule = getClockRule();
        int i = sysPersonnelClockService.updateCurrentDateClocks();
        return ResultUtil.success("成功定时更新"+i+"条数据");
    }

    @GetMapping("/replacementCard")
    @ResponseBody
    public Result<String> replacementCard(){
        System.out.println("不咔咔咔咔咔咔咔卡！");
        return ResultUtil.success("补卡成功！");
    }

    /**
     * 获取带当前年份和租户id的打卡规则
     * @return clockRule
     */
    public ClockRule getClockRule(){
        ClockRule clockRule = new ClockRule();
        clockRule.setSuitYear(DateUtils.getCurentYear());
        clockRule.setTenantId(Integer.toUnsignedLong(ShiroUtils.getSysUser().getTenantId()));
        return clockRule;
    }

}
